<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH . 'constants.inc');
    require_once('page.inc');    
	require_once(DB_PATH.'db.inc');	
	require_once(CLASSES_PATH . 'Firma.class.php');
    require_once(CLASSES_PATH . 'Contact.class.php');

	require_once('utils.inc');
	session_start();
	select_language();
	
	$function = $_GET['f'];
	
	switch ($function) {
	     
		case "arata_firme_hp": {
		    $cond_partiala = $_GET['cond_partiala'];
		    
		    if ($cond_partiala == 'ultime_adaugate')
			     $_SESSION['query_firme_hp'] = ' ORDER BY id DESC LIMIT ' . CT_MAX_SHOW_FIRME;
			break;
		}			
	     
		case "render_contact_table": {
			render_contact_table($_SESSION['firma']);
			break;
		}			
		
		case "render_cont_table": {
            $tip_cont = $_GET['tip_cont'];
            if ($tip_cont == CT_CONT_CLIENT)
                render_cont_table($_SESSION['client'], false, $currencies, $tip_cont);
            else
			    render_cont_table($_SESSION['firma'], false, $currencies, $tip_cont);
			break;
		}	

		case "render_rute_table": {
			render_rute_table($_SESSION['firma']);
			break;			
		}
		
		case "render_adresa": {			
			$tip = $_GET['tip'];
			$tip_loc = $_GET['tip_loc'];
			$id_rezultat = $_GET['id_rezultat'];
     
            if ($tip_loc == CT_ADRESA_COMANDA_INCARCARE || $tip_loc == CT_ADRESA_COMANDA_DESCARCARE) {
                $Width = DIM_Edit_Adresa_Incarcare_Descarcare_Latime;
                $Height = DIM_Edit_Adresa_Incarcare_Descarcare_Inaltime;
            }
            else {
                $Width = DIM_Edit_Adresa_Vama_Import_Export_Latime;
                $Height = DIM_Edit_Adresa_Vama_Import_Export_Inaltime;
            }
            
			render_adresa($_SESSION['comanda'], $tip_loc, $tip, $id_rezultat);
			break;
		}		
		
		case "ajax_create_new_ruta": {
            require_once('server_validations.inc');
			$loc1 = $_GET['loc1'];
			$loc2 = $_GET['loc2'];
			
			$optiune = $_GET['optiune'];
			$index = $_GET['index'];

			$raspuns_error = "";
			if (!validPrintable($loc1, 1, CT_MAX_RUTE_LOC_LENGTH)) {
				 $raspuns_error .= "td_loc1;";
			}			
			if (!validPrintable($loc2, 1, CT_MAX_RUTE_LOC_LENGTH)) {
				 $raspuns_error .= "td_loc2;";
			}	
			
			if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;'.$raspuns_error;
            else
                create_new_ruta_firma($loc1, $loc2, $optiune, $index);
                
			break;			
		}
		
		case "ajax_create_new_contact": {
            require_once('server_validations.inc');
			$contact_name = $_GET['contact_name'];
			$contact_sex = $_GET['contact_sex'];
			$contact_functie = $_GET['contact_functie'];
			$contact_tel_fix = $_GET['contact_tel_fix'];
			$contact_tel_fax = $_GET['contact_tel_fax'];
			$contact_tel_mobil = $_GET['contact_tel_mobil'];
			$contact_email = $_GET['contact_email'];
			
			$optiune = $_GET['optiune'];
			$index = $_GET['index'];

			$raspuns_error = "";
			if (!validPrintable($contact_name, 1, CT_MAX_NUME_CONTACT_LENGTH)) {
				 $raspuns_error .= "td_contact_nume;";
			}			
			if ($contact_sex != 0 && $contact_sex != 1) {
				 $raspuns_error .= "td_contact_sex;";
			}		
			if (!validPrintable($contact_functie, 0, CT_MAX_FUNCTIE_LENGTH)) {
				 $raspuns_error .= "td_contact_functie;";
			}		
			if (!validPhone($contact_tel_fix, false)) {
				 $raspuns_error .= "td_contact_tel_fix;";
			}		
			if (!validPhone($contact_tel_fax, false)) {
				 $raspuns_error .= "td_contact_tel_fax;";
			}		
			if (!validPhone($contact_tel_mobil, false)) {
				 $raspuns_error .= "td_contact_tel_mobil;";
			}		
			if (!validMail($contact_email) && $contact_email != '') {
				 $raspuns_error .= "td_contact_email;";
			}
			
			if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;'.$raspuns_error;
            else
                create_new_contact($contact_name, $contact_sex, $contact_functie, $contact_tel_fix, $contact_tel_fax, $contact_tel_mobil, $contact_email, $optiune, $index);
                
			break;
		}			
		
		case "ajax_create_new_adresa": {
            require_once('server_validations.inc');
            $tip = $_GET['tip'];
            $adrRequired = $_GET['adrRequired'];   
            $tip_loc = $_GET['tip_loc'];
            $index = $_GET['index'];
            $optiune = $_GET['optiune'];
            
            $adresa = $_GET['adresa'];
            $oras = $_GET['oras'];
            $judet = $_GET['judet'];
            $cod_postal = $_GET['cod_postal'];
            $nume_firma = $_GET['nume_firma'];
            $tara = $_GET['tara'];
            $nume_contact = $_GET['nume_contact'];
            $tel_contact = $_GET['tel_contact'];
            $alte_mentiuni = $_GET['alte_mentiuni'];
            
            if ($tip != CT_ADRESA_COMANDA || ($tip_loc != CT_ADRESA_COMANDA_DESCARCARE && $tip_loc != CT_ADRESA_COMANDA_INCARCARE
                 && $tip_loc != CT_ADRESA_COMANDA_VAMA_EXPORT && $tip_loc != CT_ADRESA_COMANDA_VAMA_IMPORT)) {
                echo "ERROR;".ERROR_INSERT_ADRESA_FOR_COMANDA_MODAL;
                break;
            }
			
            if ($tip_loc != CT_ADRESA_COMANDA_INCARCARE)
                $legatura_incarcare = $_GET['legatura_incarcare'];
                
			if ($tip_loc == CT_ADRESA_COMANDA_DESCARCARE || $tip_loc == CT_ADRESA_COMANDA_INCARCARE) {
                $data = $_GET['data'];
                $data_efectiva = $_GET['data_efectiva'];
                $tip_marfa = $_GET['tip_marfa'];
                $nr_colete = prepareNumberForServer($_GET['nr_colete']);
                $greutate = prepareNumberForServer($_GET['greutate']);
            }
            else {
                $nume_birou_vamal = $_GET['nume_birou_vamal'];
                $cod_vama = $_GET['cod_vama'];
                $impexp = $_GET['impexp'];
            }
            
			$raspuns_error = "";
            if (!validPrintable($adresa, 0, CT_MAX_ADRESA_LENGTH))
                $raspuns_error .= "td_adresa;";
                    
			if (!validPrintable($oras, 1, CT_MAX_ORAS_LENGTH)) {
				 $raspuns_error .= "td_oras;";
			}			
			if (!validPrintable($judet, 0, CT_MAX_JUDET_LENGTH)) {
				 $raspuns_error .= "td_judet;";
			}			
			if (!validPrintable($cod_postal, 0, CT_MAX_COD_POSTAL_LENGTH)) {
				 $raspuns_error .= "td_cod_postal;";
			}		    
            if (!validPrintable($nume_firma, 0, CT_MAX_NUME_FIRMA_LENGTH)) {
                 $raspuns_error .= "td_nume_firma;";
            }
			if (!validPrintable($tara, 1, CT_MAX_TARA_LENGTH)) {
				 $raspuns_error .= "td_tara;";
			}
            if (!validPrintable($nume_contact, 0, CT_MAX_NUME_CONTACT_LENGTH)) {
                 $raspuns_error .= "td_nume_contact;";
            }
            if (!validPrintable($tel_contact, 0, CT_MAX_TEL)) {
                 $raspuns_error .= "td_tel_contact;";
            }
            if (!validPrintable($alte_mentiuni, 0, CT_MAX_ALTE_MENTIUNI_LENGTH)) {
                 $raspuns_error .= "td_alte_mentiuni;";
            }
            
            if ($tip_loc == CT_ADRESA_COMANDA_DESCARCARE || $tip_loc == CT_ADRESA_COMANDA_INCARCARE) {
                $aux = checkServerDateTime(getDateForServer($data, true));
                if ($aux == FALSE && $data != '') {
                    $raspuns_error .= "td_data;";
                }
                $aux = checkServerDateTime(getDateForServer($data_efectiva, true));
                if ($aux == FALSE && $data_efectiva != '') {
                    $raspuns_error .= "td_data_efectiva;";
                }
                if (!validPrintable($tip_marfa, 0, CT_MAX_FEL_MARFA_LENGTH)) {
                     $raspuns_error .= "td_tip_marfa;";
                }
                $aux = filter_var($nr_colete, FILTER_VALIDATE_FLOAT);
                if ($aux === FALSE && $nr_colete != '') {
                     $raspuns_error .= "td_nr_colete;";
                }
                if (!validPrintable($greutate, 0, CT_MAX_GREUTATE_LENGTH)) {
                     $raspuns_error .= "td_greutate;";
                }
            }
            else {
                if (!validPrintable($nume_birou_vamal, 0, CT_MAX_NUME_BIROU_VAMAL_LENGTH)) {
                     $raspuns_error .= "td_nume_birou_vamal;";
                }
                if (!validPrintable($cod_vama, 0, CT_MAX_COD_VAMA_LENGTH)) {
                     $raspuns_error .= "td_cod_vama;";
                }
                if (!validPrintable($impexp, 0, CT_MAX_NUME_FIRMA_LENGTH)) {
                     $raspuns_error .= "td_impexp;";
                }            
            }
						
            if (strlen($raspuns_error) > 0)
                echo "VALIDATE;".$raspuns_error;
            else
			    create_new_address($tip, $tip_loc, $optiune, $adresa, $oras, $judet, $cod_postal, $tara, $nume_firma
                , $impexp, $index, $data, $data_efectiva, $nume_contact, $tel_contact, $alte_mentiuni, $nume_birou_vamal
                , $cod_vama, $tip_marfa, $nr_colete, $greutate, $legatura_incarcare);
			break;
		}
        
		case "ajax_create_new_cont": {
            require_once('server_validations.inc');
			$cont = $_GET['cont'];
			$banca = $_GET['banca'];
            $sucursala = $_GET['sucursala'];
			$moneda = $_GET['moneda'];
			
			$raspuns_error = "";
			if (!validPrintable($cont, 1, CT_MAX_CONT_LENGTH)) {
				 $raspuns_error .= "td_cont;";
			}
			if (!validPrintable($banca, 1, CT_MAX_BANCA_LENGTH)) {
				 $raspuns_error .= "td_banca;";
			}
			if (!validPrintable($sucursala, 1, CT_MAX_SUCURSALA_LENGTH)) {
				 $raspuns_error .= "td_sucursala;";
			}
            
            $aux = filter_var($moneda, FILTER_VALIDATE_INT);
            if ($aux === FALSE || $moneda < 0 || $moneda > count($currencies))
                $raspuns_error .= "td_moneda;";
			
			if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;'.$raspuns_error;
            else {
			    $optiune = $_GET['optiune'];
                $index = $_GET['index'];
			    $tip_cont = $_GET['tip_cont'];
			    create_new_cont($cont, $banca, $sucursala, $moneda, $optiune, $index, $tip_cont);			    
            }
			break;
		}
        
		case "sterge_contact": {
			$index = $_GET['index'];
			$db = db_connect();
			try {
				$inUse = sterge_contact($db, $index);
				if ($inUse) {
					echo "MESSAGE;".LANG_Contactul_nu_poate_fi_sters.";";
				}                
                render_contact_table($_SESSION['firma']);
			}
			catch (QueryLoadComandaException $e) {
				echo "ERROR;".ERROR_STERGE_CONTACT;
			}
			break;
		}
		
		case "sterge_rute_firme": {
			$index = $_GET['index'];
			$db = db_connect();
			
			$_SESSION['firma']->rute[$index]->status = CT_RUTA_FIRMA_DELETED;
			render_rute_table($_SESSION['firma']);
			break;			
		}
        
        case "sterge_cont": {
            $index = $_GET['index'];
            $tip_cont = $_GET['tip_cont'];
            sterge_cont($index, $tip_cont);
            
			if ($tip_cont == CT_CONT_CLIENT)
				render_cont_table($_SESSION['client'], false, $currencies, $tip_cont);
            else
				render_cont_table($_SESSION['firma'], false, $currencies, $tip_cont);
            break;
        }	
		
		case "sterge_adresa": {
			$index = $_GET['index'];
			$tip = $_GET['tip'];
			$tip_loc = $_GET['tip_loc'];
			$id_rezultat = $_GET['id_rezultat'];

			sterge_adresa($tip, $tip_loc, $index);
			render_adresa($_SESSION['comanda'], $tip_loc, $tip, $id_rezultat);
			break;
		}		
		
		case "arata_contact_comanda": {
			$id_firma = $_GET['id_firma'];					
			$nume_unde = "id_" . $_GET['nume_unde'];					
			$db = db_connect();	
			try {
			    render_contact_select_comanda($id_firma, -1, $db, $nume_unde);	
            }
            catch (QueryLoadContactException $e) {
                echo "ERROR;".ERROR_ARATA_CONTACT_COMANDA;
            }
            
			break;
		}
		
		case "render_firme": {
			
			$tip_firma = $_GET['tip_firma'];
			$per_pag = $_GET['per_pag'];		
			$id_client = $_SESSION['user_logat']->id_client;
			$pagina = $_GET['pagina'];
			$camp = $_GET['camp'];
			$camp_order = $_GET['camp_order'];			
			$keyword = trim($_GET['keyword']);
			$order = $_GET['order'];
			
			$val1 = $_GET['val1'];
			$val2 = $_GET['val2'];
			
			$db = db_connect();	
            
			$result = render_firme($db, $tip_firma, $per_pag, $id_client, $pagina, $camp, $order, $_SESSION['user_logat'], $keyword, $camp_order, $val1, $val2);
            
            if ($result === FALSE)
                echo "ERROR;".ERROR_RENDER_FIRME;
                
			break;
		}	
		
		case "ajax_create_new_dash": {
            require_once('server_validations.inc');
			$db = db_connect();
            
			$tip = $_GET['tip'];
			$optiune = $_GET['optiune'];
			$index = $_GET['index'];
			
			$id_client = $_SESSION['user_logat']->id_client;
			$id_user = $_SESSION['user_logat']->id_user;
			
			$tara_plecare = $_GET['tara_plecare'];
			$oras_plecare = $_GET['oras_plecare'];
			$data_plecare = getDateForServer($_GET['data_plecare'], true);

			$tara_sosire = $_GET['tara_sosire'];
			$oras_sosire = $_GET['oras_sosire'];
			$data_sosire = getDateForServer($_GET['data_sosire'], true);
			
			$comentariu = $_GET['comentariu'];
            $pret = prepareNumberForServer($_GET['pret']);
			$moneda = $_GET['moneda'];
			$km = prepareNumberForServer($_GET['km']);
            
            if (!validPrintable($tara_plecare, 0, CT_MAX_TARA_LENGTH))
                 $raspuns_error .= "td_tara_plecare;";

            if (!validPrintable($oras_plecare, 0, CT_MAX_ORAS_LENGTH))
                 $raspuns_error .= "td_oras_plecare;";
            
            $aux = checkServerDateTime($data_plecare);
            if ($aux == FALSE && $data_plecare != '') 
                $raspuns_error .= "td_data_plecare;";
            
            if (!validPrintable($tara_sosire, 0, CT_MAX_TARA_LENGTH))
                 $raspuns_error .= "td_tara_sosire;";

            if (!validPrintable($oras_sosire, 0, CT_MAX_ORAS_LENGTH))
                 $raspuns_error .= "td_oras_sosire;";
            
            $aux = checkServerDateTime($data_sosire);
            if ($aux == FALSE && $data_sosire != '') 
                $raspuns_error .= "td_data_sosire;";
                
            if (!validPrintable($comentariu, 0, CT_MAX_TEXT2_LENGTH))
                 $raspuns_error .= "td_comentariu;";
                
            $aux = filter_var($pret, FILTER_VALIDATE_FLOAT);
            if ($aux === FALSE && $pret != '')
                 $raspuns_error .= "td_pret;";
            
            $aux = filter_var($moneda, FILTER_VALIDATE_INT);
            if ($aux === FALSE || $moneda < 0 || $moneda > count($currencies))
                $raspuns_error .= "td_moneda;";
            
            $aux = filter_var($km, FILTER_VALIDATE_INT);
            if ($aux === FALSE && $km != '')
                 $raspuns_error .= "td_km;";
            
			if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;'.$raspuns_error;
            else {
			    if ($optiune == 'insert') {
				    try {
					    $dash = new Dashboard($id_client, $id_user, $tip, $tara_plecare, $oras_plecare, $data_plecare, $tara_sosire, $oras_sosire, $data_sosire, $comentariu, $pret, $km, $moneda);							
					    $dash->queryInsert($db);
				    }
				    catch (QueryInsertDashboardException $e) {
					    echo "ERROR;" . ERROR_INSERT_DASHBOARD;
				    }			
			    }
			    else {				
				    try {			
					    $dash = Dashboard::queryGetByID($db, $id_client, $index);
					    $dash->tara_plecare = $tara_plecare;
					    $dash->oras_plecare = $oras_plecare;
					    $dash->data_plecare = $data_plecare;
					    $dash->tara_sosire = $tara_sosire;
					    $dash->oras_sosire = $oras_sosire;
					    $dash->data_sosire = $data_sosire;
					    $dash->comentariu = $comentariu;
                        $dash->pret_total = $pret;
					    $dash->moneda = $moneda;
					    $dash->km = $km;
					    $dash->queryUpdate($db, $id_client, $index);										
				    }
				    catch (QueryUpdateDashboardException $e) {
					    
					    echo "ERROR;" . ERROR_UPDATE_DASHBOARD;
				    }					
			    }
            }
            break;
		}

		case "render_dasboard": {
			$tip = $_GET['tip'];
			$id_client = $_SESSION['user_logat']->id_client;
			
			render_dashboard($tip, $id_client);
			break;
		}
		
		case "render_comenzi": {
			
			if (isset($_GET['per_pag'])) $per_pag = $_GET['per_pag']; 
			else $per_pag = $_SESSION['user_logat']->comenzi_per_pag;

			$id_client = $_SESSION['user_logat']->id_client;
			if (isset($_GET['pagina'])) $pagina = $_GET['pagina']; else $pagina = null;
            
            if (isset($_GET['comanda_incasata'])) $comanda_incasata = $_GET['comanda_incasata']; else $comanda_incasata = null;
            if (isset($_GET['comanda_platita'])) $comanda_platita = $_GET['comanda_platita']; else $comanda_platita = null;
            if (isset($_GET['scadenta_incasare'])) $scadenta_incasare = $_GET['scadenta_incasare']; else $scadenta_incasare = null;
            if (isset($_GET['scadenta_plata'])) $scadenta_plata = $_GET['scadenta_plata']; else $scadenta_plata = null;
            if (isset($_GET['f_sol'])) $f_sol = $_GET['f_sol']; else $f_sol = null;
            if (isset($_GET['f_exec'])) $f_exec = $_GET['f_exec']; else $f_exec = null;
            
            if (isset($_GET['camp'])) $camp = $_GET['camp']; else $camp = null;
            if (isset($_GET['camp_sort'])) $camp_sort = $_GET['camp_sort']; else $camp_sort = null;
            if (isset($_GET['order'])) $order = $_GET['order']; else $order = null;
            if (isset($_GET['keyword'])) $keyword = $_GET['keyword']; else $keyword = null;
            
            if (isset($_GET['val1'])) $val1 = $_GET['val1']; else $val1 = null;
            if (isset($_GET['val2'])) $val2 = $_GET['val2']; else $val2 = null;
            
            //campuri ce vin din cautarea avansata
            if (isset($_GET['pret_min'])) $pret_min = $_GET['pret_min']; else $pret_min = null;
            if (isset($_GET['pret_max'])) $pret_max = $_GET['pret_max']; else $pret_max = null;
            if (isset($_GET['cost_min'])) $cost_min = $_GET['cost_min']; else $cost_min = null;
            if (isset($_GET['cost_max'])) $cost_max = $_GET['cost_max']; else $cost_max = null;
            if (isset($_GET['data_scadenta_sol1'])) $data_scadenta_sol1 = $_GET['data_scadenta_sol1']; else $data_scadenta_sol1 = null;
            if (isset($_GET['data_scadenta_sol2'])) $data_scadenta_sol2 = $_GET['data_scadenta_sol2']; else $data_scadenta_sol2 = null;
            if (isset($_GET['data_scadenta_exec1'])) $data_scadenta_exec1 = $_GET['data_scadenta_exec1']; else $data_scadenta_exec1 = null;
            if (isset($_GET['data_scadenta_exec2'])) $data_scadenta_exec2 = $_GET['data_scadenta_exec2']; else $data_scadenta_exec2 = null;
            if (isset($_GET['data_factura_sol1'])) $data_factura_sol1 = $_GET['data_factura_sol1']; else $data_factura_sol1 = null;
            if (isset($_GET['data_factura_sol2'])) $data_factura_sol2 = $_GET['data_factura_sol2']; else $data_factura_sol2 = null;
            if (isset($_GET['data_factura_exec1'])) $data_factura_exec1 = $_GET['data_factura_exec1']; else $data_factura_exec1 = null;
            if (isset($_GET['data_factura_exec2'])) $data_factura_exec2 = $_GET['data_factura_exec2']; else $data_factura_exec2 = null;

            if (isset($_GET['sc_incasare'])) $sc_incasare = $_GET['sc_incasare']; else $sc_incasare = null;
            if (isset($_GET['sc_plata'])) $sc_plata = $_GET['sc_plata']; else $sc_plata = null;
            if (isset($_GET['cond_firme'])) $cond_firme = $_GET['cond_firme']; else $cond_firme = null;

            $keyword = trim($keyword);
            if (isset($_GET['sau_si'])) $sau_si = $_GET['sau_si']; else $sau_si = null;
			$db = db_connect();	
            
            try {
                User::UpdateCampUser($db, $_SESSION['user_logat']->id_user, 'comenzi_per_pag', $per_pag);
                $_SESSION['user_logat'] = User::queryLoad($db, $_SESSION['user_logat']->id_user);
            }
            catch (QueryUpdateUserException $e) {
                echo "ERROR;" . ERROR_RENDER_COMENZI;
            }
			
			$result = render_comenzi($db, $per_pag, $pagina, $comanda_incasata, $comanda_platita, $scadenta_incasare, $scadenta_plata, $f_sol, $f_exec, $keyword, $val1, $val2, $camp, $order, $_SESSION['user_logat']->coloane_comenzi, $pret_min, $pret_max, $cost_min, $cost_max, $data_scadenta_sol1, $data_scadenta_sol2, $data_scadenta_exec1, $data_scadenta_exec2, $data_factura_sol1, $data_factura_sol2, $data_factura_exec1, $data_factura_exec2, $sc_incasare, $sc_plata, $cond_firme, $camp_sort, $currencies, $sau_si);
            if ($result === FALSE)
                echo "ERROR;" . ERROR_RENDER_COMENZI;
			break;
		}			
		
		case "render_firme_pagini": {
			$tip_firma = $_GET['tip_firma'];
			$per_pag = $_GET['per_pag'];

			$id_client = $_SESSION['user_logat']->id_client;
			$db = db_connect();	
			$result = render_firme_pagini($db, $tip_firma, $per_pag, $id_client);
            if ($result === FALSE)
                echo "ERROR;".ERROR_RENDER_FIRME_PAGINI;
                
			break;
		}	
        
		case "sterge_firma": {
			$id_firma = $_GET['id_firma'];
			$id_client = $_SESSION['user_logat']->id_client;
			$db = db_connect();
			//stergem si conturi si contacte si adresa	
			
			$result = sterge_firma($db, $id_firma, $id_client);
            if ($result === FALSE)
                echo "ERROR;".ERROR_STERGE_FIRMA;
			break;
		}
        
		case 'sterge_comanda_din_view': {
			$id_comanda = $_GET['id_comanda'];
			$db = db_connect();
			try {
		        Comanda::queryDeleteById($db, $id_comanda);
		    }
		    catch (QueryDeleteComandaException $e) {
		        echo "ERROR;" . ERROR_STERGE_COMANDA;;
		    }
					
			break;				
		}
		
		case "sterge_comanda": {
			$id_comanda = $_GET['id_comanda'];

			$id_client = $_SESSION['user_logat']->id_client;
			$per_pag = $_GET['per_pag'];
			$pagina = $_GET['pagina'];
            $comanda_incasata = $_GET['comanda_incasata'];
            $comanda_platita = $_GET['comanda_platita'];
            $scadenta_incasare = $_GET['scadenta_incasare']; 
            $scadenta_plata = $_GET['scadenta_plata'];
            $f_sol = $_GET['f_sol'];
            $f_exec = $_GET['f_exec'];
            $keyword = $_GET['keyword'];
            $val1 = $_GET['val1'];
            $val2 = $_GET['val2'];
            $camp = $_GET['camp'];
            $order = $_GET['order'];

            $pret_min = $_GET['pret_min'];
            $pret_max = $_GET['pret_max'];
            $cost_min = $_GET['cost_min'];
            $cost_max = $_GET['cost_max'];
            $data_scadenta_sol1 = $_GET['data_scadenta_sol1'];
            $data_scadenta_sol2 = $_GET['data_scadenta_sol2'];
            $data_scadenta_exec1 = $_GET['data_scadenta_exec1'];
            $data_scadenta_exec2 = $_GET['data_scadenta_exec2'];
            $data_factura_sol1 = $_GET['data_factura_sol1'];
            $data_factura_sol2 = $_GET['data_factura_sol2'];
            $data_factura_exec1 = $_GET['data_factura_exec1'];
            $data_factura_exec2 = $_GET['data_factura_exec2'];
//            $incasata = $_GET['incasata'];
//            $platita = $_GET['platita'];
            $sc_incasare = $_GET['sc_incasare'];
            $sc_plata = $_GET['sc_plata'];
            $cond_firme = $_GET['cond_firme'];
            $camp_sort = $_GET['camp_sort'];
			$coloane_comenzi = $_SESSION['user_logat']->coloane_comenzi;
            
			$db = db_connect();	
                      
			$result = sterge_and_render_comenzi($db, $id_comanda, $id_client, $per_pag, $pagina, $comanda_incasata, $comanda_platita, $scadenta_incasare, $scadenta_plata, $f_sol, $f_exec, $keyword, $val1, $val2, $camp = null, $order = null, $coloane_comenzi, $pret_min, $pret_max, $cost_min, $cost_max, $data_scadenta_sol1, $data_scadenta_sol2, $data_scadenta_exec1, $data_scadenta_exec2, $data_factura_sol1, $data_factura_sol2, $data_factura_exec1, $data_factura_exec2, $sc_incasare, $sc_plata, $cond_firme, $camp_sort);
            
            if ($result === FALSE)
                echo "ERROR;".ERROR_STERGE_COMANDA;
			
			break;
		}
		
		case "render_users_table": {
			render_users_table($_SESSION['users']);
			break;
		}
        
		case "ajax_create_new_user": {
            require_once('server_validations.inc');
            require_once(CLASSES_PATH . 'phpmailer/class.mail.php');
            
			$username = $_GET['username'];
			$nume = $_GET['nume'];
			$prenume = $_GET['prenume'];
			$email = $_GET['email'];
            $permissionsTextForEmail = "";
			
            $raspuns_error = "";
            $tip = 0;
            
            if (isset($_GET['perm_comenzi']) && $_GET['perm_comenzi'] == 1)
                $tip = $tip | CT_PERM_COMENZI;
            else
                if (isset($_GET['perm_comenzi']))
                    $raspuns_error .= "td_perm_comenzi;";          
            
            if (isset($_GET['perm_comenzi_dupa_confirmare']) && $_GET['perm_comenzi_dupa_confirmare'] == 1)
                $tip = $tip | CT_PERM_COMENZI_DUPA_CONFIRMARE;
            else
                if (isset($_GET['perm_comenzi_dupa_confirmare']))
                    $raspuns_error .= "td_perm_comenzi_dupa_confirmare;";          
            
            if (isset($_GET['perm_firme']) && $_GET['perm_firme'] == 1)
                $tip = $tip | CT_PERM_FIRME;
            else
                if (isset($_GET['perm_firme']))
                    $raspuns_error .= "td_perm_firme;";
                
            if (isset($_GET['perm_edit_clienti']) && $_GET['perm_edit_clienti'] == 1)
                $tip = $tip | CT_PERM_EDIT_CLIENT;
            else
                if (isset($_GET['perm_edit_clienti']))
                    $raspuns_error .= "td_perm_edit_client;";
                
            if (isset($_GET['perm_edit_users']) && $_GET['perm_edit_users'] == 1)
                $tip = $tip | CT_PERM_EDIT_USERS;
            else
                if (isset($_GET['perm_edit_users']))
                    $raspuns_error .= "td_perm_edit_users;";
                
            if (isset($_GET['perm_plati']) && $_GET['perm_plati'] == 1)
                $tip = $tip | CT_PERM_PLATI;
            else
                if (isset($_GET['perm_plati']))
                    $raspuns_error .= "td_perm_plati;";
                
            if (isset($_GET['perm_statistici']) && $_GET['perm_statistici'] == 1)
                $tip = $tip | CT_PERM_STATISTICI;
            else
                if (isset($_GET['perm_statistici']))
                    $raspuns_error .= "td_perm_statistici;";
                
            if (isset($_GET['perm_admin_abonament']) && $_GET['perm_admin_abonament'] == 1)
                $tip = $tip | CT_PERM_ADMIN_ABONAMENT;
            else
                if (isset($_GET['perm_admin_abonament']))
                    $raspuns_error .= "td_perm_admin_abonament;";                
            
            if (!validPrintable($username, 1, CT_MAX_USERNAME_LENGTH) || strstr($username, " ")) {
                 $raspuns_error .= "td_username;";
            }
            if (!validPrintable($nume, 1, CT_MAX_NUME_LENGTH)) {
                 $raspuns_error .= "td_nume;";
            }
            if (!validPrintable($prenume, 1, CT_MAX_PRENME_LENGTH)) {
                 $raspuns_error .= "td_prenume;";
            }
            if (!validMail($email) || $email == '') {
                 $raspuns_error .= "td_email;";
            }
            
            if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;'.$raspuns_error;
            else {
			    $optiune = $_GET['optiune'];
			    $index = $_GET['index'];
			    $db = db_connect();
				$result = create_new_user($db, $_SESSION['client_logat']->id_client, $username, $nume, $prenume, $email, $tip, $optiune, $index);

                if ($result === FALSE)
                    echo "ERROR;".ERROR_AJAX_CREATE_NEW_USER;
                elseif ($result === CT_DUPLICATE_USERNAME) {
                    echo "MESSAGE;" . LANG_Duplicate_Username.";";
                }
                elseif ($result === CT_DUPLICATE_EMAIL) {
                    echo "MESSAGE;" . LANG_Duplicate_Email.";";
                }
                else if ($optiune != 'update') { 
                	/*
                	 * sa nu se mai trimita update decat in cazul cand se adauga un nou utilizator, nu si la update
                	 */         	
                	$tempUserObject = array_pop($_SESSION['users']);
                    sendEmailForNewUser(LANG_Body_New_User_Email, $username, $tempUserObject->nonCryptedPassword, $email);
                    array_push($_SESSION['users'], $tempUserObject);
                } 
            }            
			break;
		}
		
		case "sterge_user": {
			$index = $_GET['index'];
			$db = db_connect();	
			
			if (($result = sterge_user($db, $index)) === FALSE)
                echo "ERROR;".ERROR_STERGE_USER;
            
			render_users_table($_SESSION['users']);
			break;
		}	

		case "renderPlatiPage": {						
			require_once("utilsPlati.inc");
			global $totals;
			global $orderBY;
			global $orderKIND;
			
			$db = db_connect();		
			
			$pageNumber = $_GET['page'];
			$perPage 	= $_GET['perPage'];			
			$orderBY 	= $_GET['orderby'];
			$orderKIND 	= $_GET['orderkind'];
			$tip 		= $_GET['tip'];		
			$default  	= $_GET['default'];
			//var_dump($_GET);
			
			$offset = ($pageNumber - 1) * $perPage;								
			
			try {			
				//is it a default view?
				if ($default == "0") {		
					$plati = getLastNPlati($db, $tip, $perPage, $offset, $orderBY, $orderKIND, $_SESSION['client_logat']->id_client);																				
					renderPlatiTable($tip, $plati, $perPage, $default, $pageNumber);																			
				}
				//we have a filtration :D
				else {
                    isset($_GET['moneda']) ? $moneda = $_GET['moneda'] : $moneda = "";
					isset($_GET['suma']) ? $suma = prepareNumberForServer($_GET['suma']) : $suma = "";
					isset($_GET['data']) ? $data = getDateForServer($_GET['data']) : $data = "";
					isset($_GET['nr_comanda']) ? $nr_comanda = $_GET['nr_comanda'] : $nr_comanda = "";
                    
					renderPlatiTable($tip, getLastNPlati($db, $tip, $perPage, $offset, $orderBY, $orderKIND, $_SESSION['client_logat']->id_client, $moneda, $suma, $nr_comanda, $data), $perPage, 1, $pageNumber);
				}
				//throw new QueryGetLast20PlatiException("test");
			}
			catch (QueryGetLast20PlatiException $e) {	
				echo "ERROR;".ERROR_RENDER_PLATI_PAGE;
			}
			break;		
		}
		
		case "deletePlata": {	
			require_once "utilsPlati.inc";
			
			global $totals;
			$db = db_connect();		

			$perPage 		= $_GET['perPage'];
			$defaultView 	= $_GET['default'];
			$pageNumber 	= $_GET['pageNumber'];
			$tip 			= $_GET['tip'];
			$nrComanda      = $_GET['numar_comanda'];
            
			try {  										
				$db->beginTransaction();	
			
				PlatiComanda::queryDeleteById($db, $_GET['id']);					    													
				Comanda::queryUpdateStareComanda($db, $_GET['id'], $tip);								
				
				$db->commit();	
				
				$offset = ($pageNumber - 1) * $perPage;									
				$platiBuffer = getLastNPlati($db, $tip, $perPage, $offset, "suma", 1, $_SESSION['client_logat']->id_client, null, null, $nrComanda);
				
				if (count($platiBuffer) == 0) {
					$pageNumber = $_GET['pageNumber'] - 1;
					if ($pageNumber == 0) $pageNumber = 1;	
					$offset = ($pageNumber - 1) * $perPage;
                    
                    $platiBuffer = getLastNPlati($db, $tip, $perPage, $offset, "suma", 1, $_SESSION['client_logat']->id_client, null, null, $nrComanda);
				}
				
				renderPlatiTable($tip, $platiBuffer, $perPage, $defaultView, $pageNumber, $nrComanda);
			}
			catch (QueryDeletePlatiComandaException $e) {
				$db->rollBack();
				echo "ERROR;".ERROR_DELETE_PLATA;
			}
			catch (QueryUpdateStareComandaException $e) {
				$db->rollBack();
				echo "ERROR;".ERROR_DELETE_PLATA;
			}
			break;
		}
		
		case "print_firme": {						
			$indecsi = $_GET['indecsi'];
			if (($result = print_firme($indecsi)) === FALSE)
                echo "ERROR;".ERROR_PRINT_FIRME;
            
			break;
		}
        
        case "print_factura": {						
			$index = $_GET['index'];
			if (($result = print_factura($index)) === FALSE)
                echo "ERROR;".ERROR_PRINT_FACTURA;
            
			break;
		}
        
		case "update_profile": {	
			$db = db_connect();		
								
			$camp = $_GET['camp'];
			$value = $_GET['value'];
			$user_id = $_SESSION['user_logat']->id_user;
			
			try {
				User::UpdateCampUser($db, $user_id, $camp, $value);
				//dupa ce am facut update va trebui sa il incarc din nou in sesiune
				$_SESSION['user_logat'] = User::queryLoad($db, $_SESSION['user_logat']->id_user);
			}
			catch (QueryUpdateUserException $e) {
				echo "ERROR;".ERROR_UPDATE_PROFILE;
			}
			break;
		}
        
		case "custom_field_firma": {
			$cf = $_GET['cf'];
			$rc = $_GET['rc'];
			$tel_fix = $_GET['tel_fix'];
			$tel_fax = $_GET['tel_fax'];
			$tel_mobil = $_GET['tel_mobil'];
			$web = $_GET['web'];
			$email = $_GET['email'];

			$tip = $_GET['tip'];			
			$tara = $_GET['tara'];
			$oras = $_GET['oras'];						
			$calitate_firma = $_GET['calitate_firma'];
						
			$campuri = 'nume';
			if ($cf == 'true') $campuri .= ',cf'; 
			if ($rc == 'true') $campuri .= ',rc'; 
			if ($tel_fix == 'true') $campuri .= ',tel_fix'; 
			if ($tel_fax == 'true') $campuri .= ',tel_fax'; 
			if ($tel_mobil == 'true') $campuri .= ',tel_mobil'; 
			if ($web == 'true') $campuri .= ',web'; 
			if ($email == 'true') $campuri .= ',email'; 
			if ($tip == 'true') $campuri .= ',tip';
			if ($tara == 'true') $campuri .= ',tara';
			if ($oras == 'true') $campuri .= ',oras';
			if ($calitate_firma == 'true') $campuri .= ',calitate_firma';
						
			$db = db_connect();	
			try {
				User::UpdateCampUser($db, $_SESSION['user_logat']->id_user, 'coloane_firme', $campuri);
				$_SESSION['user_logat'] = User::queryLoad($db, $_SESSION['user_logat']->id_user);
			 
			}
			catch (QueryUpdateUserException $e) {
				echo "ERROR;".ERROR_CUSTOM_FIELD_FIRMA;
			}	
			break;					
		}
		
		case "custom_field_firma_render": {	
			$cf = $_GET['cf'];
			$rc = $_GET['rc'];
			$tel_fix = $_GET['tel_fix'];
			$tel_fax = $_GET['tel_fax'];
			$tel_mobil = $_GET['tel_mobil'];
			$web = $_GET['web'];
			$email = $_GET['email'];

			$tip = $_GET['tip'];			
			$tara = $_GET['tara'];
			$oras = $_GET['oras'];						
			
			$calitate_firma = $_GET['calitate_firma'];
			
			$campuri = 'nume';
			if ($cf == 'true') $campuri .= ',cf'; 
			if ($rc == 'true') $campuri .= ',rc'; 
			if ($tel_fix == 'true') $campuri .= ',tel_fix'; 
			if ($tel_fax == 'true') $campuri .= ',tel_fax'; 
			if ($tel_mobil == 'true') $campuri .= ',tel_mobil'; 
			if ($web == 'true') $campuri .= ',web'; 
			if ($email == 'true') $campuri .= ',email'; 
			if ($tip == 'true') $campuri .= ',tip';
			if ($tara == 'true') $campuri .= ',tara';
			if ($oras == 'true') $campuri .= ',oras';
			if ($calitate_firma == 'true') $campuri .= ',Calitate_firma';
			
			$db = db_connect();	
			try {
				User::UpdateCampUser($db, $_SESSION['user_logat']->id_user, 'coloane_firme', $campuri);
				$_SESSION['user_logat'] = User::queryLoad($db, $_SESSION['user_logat']->id_user);
			 
			}
			catch (QueryUpdateUserException $e) {
				echo "ERROR;".ERROR_CUSTOM_FIELD_FIRMA;
			}
			
			$tip_firma = $_GET['tip_firma'];
			$per_pag = $_GET['per_pag'];

			$id_client = $_SESSION['user_logat']->id_client;
			$pagina = $_GET['pagina'];
			$camp = $_GET['camp'];
			$camp_order = $_GET['camp_order'];			
			$keyword = $_GET['keyword'];
			$order = $_GET['order'];
            
			$result = render_firme($db, $tip_firma, $per_pag, $id_client, $pagina, $camp, $order, $_SESSION['user_logat'], $keyword, $camp_order);
            
            if ($result === FALSE)
                echo "ERROR;".ERROR_RENDER_FIRME;
                
			break;
		}

		case "custom_field_comand": {	
			$id_firma_sol = $_GET['id_firma_sol'];
			$nr_comanda_sol = $_GET['nr_comanda_sol'];
			$nr_factura_sol = $_GET['nr_factura_sol'];
			$suma_sol = $_GET['suma_sol'];
			$taxe_supl_to_sol = $_GET['taxe_supl_to_sol'];
			$penaliz_from_sol = $_GET['penaliz_from_sol'];
			$data_tr_acte_sol = $_GET['data_tr_acte_sol'];
			$suma_totala_sol = $_GET['suma_totala_sol'];
			$moneda_sol = $_GET['moneda_sol'];
			
			$id_firma_exec = $_GET['id_firma_exec'];
			$nr_comanda_exec = $_GET['nr_comanda_exec'];
			$nr_factura_exec = $_GET['nr_factura_exec'];
			$suma_exec = $_GET['suma_exec'];
			$taxe_supl_from_exec = $_GET['taxe_supl_from_exec'];
			$penaliz_to_exec = $_GET['penaliz_to_exec'];
			$data_primirii_acte_exec = $_GET['data_primirii_acte_exec'];
			$suma_totala_exec = $_GET['suma_totala_exec'];
			$moneda_exec = $_GET['moneda_exec'];
			
			$data_scadenta_sol = $_GET['data_scadenta_sol'];						
			$data_scadenta_exec = $_GET['data_scadenta_exec'];
			$data_factura_sol = $_GET['data_factura_sol'];
			$data_factura_exec = $_GET['data_factura_exec'];
			$data_comenzii = $_GET['data_comenzii'];
			
			$nume_sofer = $_GET['nume_sofer'];
			$tel_sofer = $_GET['tel_sofer'];
			$nr_inmatriculare = $_GET['nr_inmatriculare'];
			$fel_camion = $_GET['fel_camion'];
			
			$_cmr_exec = $_GET['_cmr_exec'];
			$_factura_exec = $_GET['_factura_exec'];
			$_carnet_tir_exec = $_GET['_carnet_tir_exec'];
			$_decl_vamala_exec = $_GET['_decl_vamala_exec'];
			$_text_alte_acte_exec = $_GET['_text_alte_acte_exec'];
			
			$_cmr_sol = $_GET['_cmr_sol'];
			$_factura_sol = $_GET['_factura_sol'];
			$_carnet_tir_sol = $_GET['_carnet_tir_sol'];
			$_decl_vamala_sol = $_GET['_decl_vamala_sol'];
			$_text_alte_acte_sol = $_GET['_text_alte_acte_sol'];
			
			$id_contact_sol = $_GET['id_contact_sol'];
			$id_contact_exec = $_GET['id_contact_exec'];
			$coment_sol = $_GET['coment_sol'];
			$coment_exec = $_GET['coment_exec'];
			
			$stare_incasare_sol = $_GET['stare_incasare_sol'];
			$stare_plata_exec = $_GET['stare_plata_exec'];
						
			$camp = $_GET['camp'];
			$order = $_GET['order'];
			$pagina = $_GET['pagina'];
			$per_pag = $_GET['per_pag'];

			$id_client = $_SESSION['user_logat']->id_client;				
			$campuri = 'id';
			
			if ($id_firma_sol == 'true') $campuri .= ',id_firma_sol'; 			
			if ($nr_comanda_sol == 'true') $campuri .= ',nr_comanda_sol';
			if ($nr_factura_sol == 'true') $campuri .= ',nr_factura_sol';
			if ($suma_sol == 'true') $campuri .= ',suma_sol';
			if ($taxe_supl_to_sol == 'true') $campuri .= ',taxe_supl_to_sol';
			if ($penaliz_from_sol == 'true') $campuri .= ',penaliz_from_sol';
			if ($data_tr_acte_sol == 'true') $campuri .= ',data_tr_acte_sol';
			if ($suma_totala_sol == 'true') $campuri .= ',suma_totala_sol';
			if ($moneda_sol == 'true') $campuri .= ',moneda_sol';
			
			if ($id_firma_exec == 'true') $campuri .= ',id_firma_exec';
			if ($nr_comanda_exec == 'true') $campuri .= ',nr_comanda_exec';
			if ($nr_factura_exec == 'true') $campuri .= ',nr_factura_exec';
			if ($suma_exec == 'true') $campuri .= ',suma_exec';
			if ($taxe_supl_from_exec == 'true') $campuri .= ',taxe_supl_from_exec';
			if ($penaliz_to_exec == 'true') $campuri .= ',penaliz_to_exec';
			if ($data_primirii_acte_exec == 'true') $campuri .= ',data_primirii_acte_exec';
			if ($suma_totala_exec == 'true') $campuri .= ',suma_totala_exec';
			if ($moneda_exec == 'true') $campuri .= ',moneda_exec';
			
			if ($data_scadenta_sol == 'true') $campuri .= ',data_scadenta_sol';						
			if ($data_scadenta_exec == 'true') $campuri .= ',data_scadenta_exec';
			if ($data_factura_sol == 'true') $campuri .= ',data_factura_sol';
			if ($data_factura_exec == 'true') $campuri .= ',data_factura_exec';
			if ($data_comenzii == 'true') $campuri .= ',data_comenzii';
			if ($data_incarcarii == 'true') $campuri .= ',data_incarcarii';
			if ($data_descarcarii == 'true') $campuri .= ',data_descarcarii';
			
			if ($nume_sofer == 'true') $campuri .= ',nume_sofer';
			if ($tel_sofer == 'true') $campuri .= ',tel_sofer';
			if ($nr_inmatriculare == 'true') $campuri .= ',nr_inmatriculare';
			if ($fel_camion == 'true') $campuri .= ',fel_camion';
			if ($fel_marfa == 'true') $campuri .= ',fel_marfa';
			if ($tonaj == 'true') $campuri .= ',tonaj';
			if ($alte_conditii == 'true') $campuri .= ',alte_conditii';
			
			if ($_cmr_exec == 'true') $campuri .= ',_cmr_exec';
			if ($_factura_exec == 'true') $campuri .= ',_factura_exec';
			if ($_carnet_tir_exec == 'true') $campuri .= ',_carnet_tir_exec';
			if ($_decl_vamala_exec == 'true') $campuri .= ',_decl_vamala_exec';
			if ($_text_alte_acte_exec == 'true') $campuri .= ',_text_alte_acte_exec';
			
			if ($_cmr_sol == 'true') $campuri .= ',_cmr_sol';
			if ($_factura_sol == 'true') $campuri .= ',_factura_sol';
			if ($_carnet_tir_sol == 'true') $campuri .= ',_carnet_tir_sol';
			if ($_decl_vamala_sol == 'true') $campuri .= ',_decl_vamala_sol';
			if ($_text_alte_acte_sol == 'true') $campuri .= ',_text_alte_acte_sol';
			
			if ($id_contact_sol == 'true') $campuri .= ',id_contact_sol';
			if ($id_contact_exec == 'true') $campuri .= ',id_contact_exec';
			if ($coment_sol == 'true') $campuri .= ',coment_sol';
			if ($coment_exec == 'true') $campuri .= ',coment_exec';
			
			if ($stare_incasare_sol == 'true') $campuri .= ',stare_incasare_sol';
			if ($stare_plata_exec == 'true') $campuri .= ',stare_plata_exec';

			//--- celebrul carnat :)))

			$tip_raspuns = $_GET['tip_raspuns'];
			$id_client = $_SESSION['user_logat']->id_client;
			$db = db_connect();
						
			if ($tip_raspuns == 'save_render') {
			
				$per_pag = $_GET['per_pag'];
				$pagina = $_GET['pagina'];	
									
				$comanda_incasata = $_GET['comanda_incasata'];
				$comanda_platita = $_GET['comanda_platita'];
				$scadenta_incasare = $_GET['scadenta_incasare'];
				$scadenta_plata = $_GET['scadenta_plata'];
				$f_sol = $_GET['f_sol'];
				$f_exec = $_GET['f_exec'];
				$keyword = $_GET['keyword'];
				$val1 = $_GET['val1'];
				$val2 = $_GET['val2'];
				$camp = $_GET['camp'];
				$camp_sort = $_GET['camp_sort'];			
				$order = $_GET['order'];
	
				$pret_min = $_GET['pret_min'];
				$pret_max = $_GET['pret_max'];
				$cost_min = $_GET['cost_min'];
				$cost_max = $_GET['cost_max'];
				$data_scadenta_sol1 = $_GET['data_scadenta_sol1'];
				$data_scadenta_sol2 = $_GET['data_scadenta_sol2'];
				$data_scadenta_exec1 = $_GET['data_scadenta_exec1'];
				$data_scadenta_exec2 = $_GET['data_scadenta_exec2'];
				$data_factura_sol1 = $_GET['data_factura_sol1'];
				$data_factura_sol2 = $_GET['data_factura_sol2'];
				$data_factura_exec1 = $_GET['data_factura_exec1'];
				$data_factura_exec2 = $_GET['data_factura_exec2'];

				$sc_incasare = $_GET['sc_incasare'];
				$sc_plata = $_GET['sc_plata'];
				$cond_firme = $_GET['cond_firme'];						
				//---
				
	
				try {
					User::UpdateCampUser($db, $_SESSION['user_logat']->id_user, 'coloane_comenzi', $campuri);
					$_SESSION['user_logat'] = User::queryLoad($db, $_SESSION['user_logat']->id_user);
				}
				catch (QueryUpdateUserException $e) {
					echo "ERROR;" . ERROR_CUSTOM_FIELD_COMAND;
				}
				$coloane_comenzi = $_SESSION['user_logat']->coloane_comenzi;			
				$arr = get_comenzi($db, $pagina, $per_pag, $comanda_incasata, $comanda_platita, $scadenta_incasare, $scadenta_plata, $f_sol, $f_exec, $keyword, $val1, $val2, $camp, $order, $coloane_comenzi, $pret_min, $pret_max, $cost_min, $cost_max, $data_scadenta_sol1, $data_scadenta_sol2, $data_scadenta_exec1, $data_scadenta_exec2, $data_factura_sol1, $data_factura_sol2, $data_factura_exec1, $data_factura_exec2, $sc_incasare, $sc_plata, $cond_firme, $camp_sort);
		        if (is_array($arr))
				    echo $arr[0];
	            else
	                echo "ERROR;".ERROR_CUSTOM_FIELD_COMAND;
			}
			if ($tip_raspuns == 'save') {
				try {
					User::UpdateCampUser($db, $_SESSION['user_logat']->id_user, 'coloane_comenzi', $campuri);
					$_SESSION['user_logat'] = User::queryLoad($db, $_SESSION['user_logat']->id_user);
				}
				catch (QueryUpdateUserException $e) {
					echo "ERROR;" . ERROR_CUSTOM_FIELD_COMAND;
				}
				$coloane_comenzi = $_SESSION['user_logat']->coloane_comenzi;			
			}
		
			break;
		}
        
        case "cauta_contact_firma": {
            $per_pag_cauta = $_GET['per_page'];
            $value_cauta = $_GET['keyword'];
            $camp_cauta = $_GET['camp'];

			$id_client = $_SESSION['user_logat']->id_client;
            if ($camp_cauta == -1) {
            	$conditie = "(LOWER(contact.nume) LIKE '%" . $value_cauta . "%'
            				OR LOWER(contact.functie) LIKE '%" . $value_cauta . "%'
            				OR LOWER(contact.tel_fix) LIKE '%" . $value_cauta . "%'
            				OR LOWER(contact.tel_fax) LIKE '%" . $value_cauta . "%'
            				OR LOWER(contact.tel_mobil) LIKE '%" . $value_cauta . "%'
            				OR LOWER(contact.email) LIKE '%" . $value_cauta . "%')";
            }
            else $conditie = "contact.$camp_cauta LIKE '%" . $value_cauta . "%'";
            $pagina = $_GET['pagina'];            
            
            try {
                $db = db_connect();
                $contacts = Contact::queryLoadByClient($db, $conditie, $id_client, $per_pag_cauta, $pagina);
            }
            catch (QueryLoadContactException $e) {
                echo "ERROR;".ERROR_CAUTA_CONTACT_FIRMA;
            }
            
            print_contacts_table($contacts[1], $contacts[0], $per_pag_cauta, $value_cauta, $camp_cauta, $id_client, $pagina);        
            break;
        }
        
        case "arata_plati_client": {
			$id_client = $_SESSION['user_logat']->id_client;
            $pagina = $_GET['pagina'];
            $per_pag = $_GET['per_pag'];
            $db = db_connect();
            
            render_plati_client($db, $pagina, $per_pag, $id_client);
            break;
        }
        
        case "introdu_sesiune": {
        	$camp = $_GET['camp'];
        	$valoare = $_GET['valoare'];
        	$_SESSION[$camp] = $valoare;
        	break;
        }
        
        case "remove_image": {
        	$camp = $_GET['camp'];
        	$valoare = $_GET['valoare'];
        	$_SESSION[$camp] = $valoare;
        	break;
        }
        
        case "ajax_print_factura": {
            require_once('server_validations.inc');
            $db = db_connect();
            
            $id = $_GET['id'];
            $saveAll = $_GET['saveAll'];
            $moneda = $_GET['moneda'];
			$moneda_comanda = $_GET['moneda_comanda'];
            $curs = prepareNumberForServer($_GET['curs']);
			$curs_invers = prepareNumberForServer($_GET['curs_invers']);
			$text_factura = $_GET['text_factura'];
			$serie_factura = $_GET['serie_factura'];
			$tva_inclus = $_GET['tva_inclus'];
            $tva = prepareNumberForServer($_GET['tva']);
            $nume_delegat = $_GET['nume_delegat'];
            $mijloc_transport = $_GET['mijloc_transport'];
            $cnp = $_GET['cnp'];
			$serie_ci = $_GET['serie_ci'];
			
            $raspuns_error = "";
					
			if ($tva_inclus != '0' && $tva_inclus != '1')
				$raspuns_error .= "td_tva_inclus;";
				
			// cel putin unul din cursuri trebuie completat.
			$curs_bun = false;
			
			$aux = filter_var($id, FILTER_VALIDATE_INT);
            if ($aux === FALSE)
                $raspuns_error .= "";
                
			$aux = filter_var($moneda, FILTER_VALIDATE_INT);
            if ($aux === FALSE || $moneda < 0 || $moneda > count($currencies))
                $raspuns_error .= "td_moneda_factura;";
            
                
            $aux = filter_var($curs, FILTER_VALIDATE_FLOAT);
			if ($aux === false)
				{}
			else
				$curs_bun = true;				
			
                
            $aux = filter_var($curs_invers, FILTER_VALIDATE_FLOAT);
			if ($aux === false)
				{}
            else
                $curs_bun = true;                
            
			if (!$curs_bun && ($moneda != $moneda_comanda)) {
                $raspuns_error .= "td_curs;";
                $raspuns_error .= "td_curs_invers;";
            }
				    
			if (!validPrintable($text_factura, 1, CT_MAX_TEXT_FACTURA))
				 $raspuns_error .= "td_text_factura;";
			
			
			if (!validPrintable($serie_factura, 1, CT_MAX_SERIE_FACTURA))
				 $raspuns_error .= "td_serie_factura;";
			
            
            $aux = filter_var($tva, FILTER_VALIDATE_INT);
            if ($aux === FALSE && $tva != '' )
                $raspuns_error .= "td_tva;";
                
            if (!validPrintable($nume_delegat, 1, CT_MAX_TEXT_NUME_DELEGAT))
                 $raspuns_error .= "td_nume_delegat;";
                 
            if (!validPrintable($mijloc_transport, 1, CT_MAX_TEXT_MIJLOC_TRANSPORT))
                 $raspuns_error .= "td_mijloc_transport;";
                 
            if (!validPrintable($cnp, 1, CT_MAX_TEXT_CNP))
                 $raspuns_error .= "td_cnp;";
                 
            if (!validPrintable($serie_ci, 1, CT_MAX_TEXT_SERIE_CI))
                 $raspuns_error .= "td_serie_ci;";
            
			
			if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;'.$raspuns_error;
            else {
				echo '';
                
                try {
                    $factura = Factura::queryLoadFacturaByCommandId($db, $id);
                    if ($factura == CT_NO_FACTURA_FOUND) {
                        $factura = new Factura($id, $moneda, $curs, $curs_invers, $text_factura, $nume_delegat, $mijloc_transport
                                                , $cnp, $serie_ci, $tva_inclus, $tva, $_SESSION['user_logat']->id_user); 
                        $factura->queryInsert($db);
                        $factura->data = date('Y-m-d');
                    }
                    else {
                        // Update-ul la campurile facturii;
                        $factura->cnp_delegat = $cnp;
                        $factura->cota_tva = $tva;
                        $factura->curs_1 = $curs;
                        $factura->curs_2 = $curs_invers;
                        $factura->mijloc_transport = $mijloc_transport;
                        $factura->moneda = $moneda;
                        $factura->nume_delegat = $nume_delegat;
                        $factura->serie_ci_delegat = $serie_ci;
                        $factura->text_factura = $text_factura;
                        $factura->tva_inclus= $tva_inclus;
//                        var_dump($factura);
                        $factura->queryUpdate($db);
                    }
                    
                    if ($saveAll == 'true')
                        Comanda::queryStaticUpdateNrFacturaSol($db, $index, $id_client, $serie_factura);
                }
                catch (Exception $e) {
                    // De facut ceva???
                }
                
			    //TODO
			    $_SESSION['print_factura_moneda'] = $moneda;
			    $_SESSION['print_factura_curs'] = $curs;
			    $_SESSION['print_factura_curs_invers'] = $curs_invers;
			    $_SESSION['print_factura_text_factura'] = $text_factura;
			    $_SESSION['print_factura_serie_factura'] = $serie_factura;
			    $_SESSION['print_factura_tva_inclus'] = $tva_inclus;
                $_SESSION['print_factura_tva'] = $tva;
                $_SESSION['print_factura_data'] = $factura->data;
                $_SESSION['print_factura_nume_delegat'] = $nume_delegat;
                $_SESSION['print_factura_mijloc_transport'] = $mijloc_transport;
                $_SESSION['print_factura_cnp'] = $cnp;
			    $_SESSION['print_factura_serie_ci'] = $serie_ci;
            }
			break;
		}
	    
		case "ajax_create_new_firma_from_modal": {
        	
            require_once('server_validations.inc');
            
			$nume_firma = $_GET['nume_firma'];
			$tel_fix = $_GET['tel_fix'];
            $tel_fax = $_GET['tel_fax'];
			$tel_mobil = $_GET['tel_mobil'];
			$email_modal = $_GET['email_modal'];
			
			$raspuns_error = "";
			if (!validPrintable($nume_firma, 1, CT_MAX_NUME_FIRMA_LENGTH)) 
				$raspuns_error .= "td_nume_firma_modal;";

			if (!validPhone($tel_fix, true) && $tel_fix != '') 
				$raspuns_error .= "td_tel_fix_modal;";
			
			if (!validPhone($tel_fax, true) && $tel_fax != '') 
				$raspuns_error .= "td_tel_fax_modal;";
			
			if (!validPhone($tel_mobil, true) && $tel_mobil != '')
				$raspuns_error .= "td_tel_mobil_modal;"; 			
			
			if (!validMail($email_modal) && $email_modal != '') 
				$raspuns_error .= "td_email_modal;";
				
			if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;' . $raspuns_error;
            else {

			    $optiune = $_GET['optiune'];
                $index = $_GET['index'];
				$id_client = $_SESSION['user_logat']->id_client;
				$db = db_connect();
				
				if ($index == -1) {
					$firma = new Firma($id_client, $nume_firma, null, null, $tel_fix, $tel_fax, $tel_mobil, $email_modal,null,CT_TIP_FIRMA_ALL);
					
					try {
						$firma->queryInsert($db);
					}
					catch (QueryInsertFirmaException $e) {
						echo "ERROR;" . ERROR_INSERT_FIRMA_MODAL;
					}
					catch (QueryInsertAddressException $e) {
						echo "ERROR;" . ERROR_INSERT_ADRESA_FOR_FIRMA_MODAL;				
					}				
				}
				else {
					try {
						Firma::queryLighUpdate($db, $id_client, $index, $nume_firma, $tel_fix, $tel_fax, $tel_mobil, $email_modal);
					}
					catch (QueryUpdateFirmaException $e) {
						echo "ERROR;" . ERROR_UPDATE_FIRMA;
					}
				}
            }
			break;
		}
		
        case "render_firme_add_comanda_combobox": {
			try {
				$id_client = $_SESSION['user_logat']->id_client;
				$db = db_connect();
				$var = Firma::queryLoadByClientWithName($db, $id_client);
				$index = $_GET['index'];
				$from = $_GET['from'];
				
				if ($from == 'id_firma_sol') $contact_from = 'contact_sol';
				else $contact_from = 'contact_exec';
				
				if ($index == -100) {
					render_firme_comanda_show_contact($from, $var, null, $contact_from);
				}
				else if ($index == "desc") {
					render_firme_comanda_show_contact($from, $var, Firma::getIdLastFirm($db, $id_client), $contact_from);
				}
				else {
					render_firme_comanda_show_contact($from, $var, $index, $contact_from);
				}
			}
			catch (QueryLoadFirmaException $e) {
				redirectOnError(CT_ADD_COMANDA_FIRMA_LOAD_BY_CLIENT_WITH_NAME);				  	
			}
			break;        	
        }
        
        case "render_contacte_add_comanda_combobox": {
			$id_client = $_SESSION['user_logat']->id_client;
			$db = db_connect();
			$id_firma = $_GET['id_firma'];
			$index = $_GET['index'];
			$from = $_GET['from'];
				
			$contacte = Contact::queryLoadByFirma($db, $id_firma);

			if ($index == 'desc') {

				render_contact_select_comanda($id_firma, Contact::getIdOfLastContactFromFirm($db, $id_firma), $db, $from);
			}
			else
				render_contact_select_comanda($id_firma, $index, $db, $from);
        	break;
        }

        case "ajax_create_new_contact_from_modal": {
			require_once('server_validations.inc');
            $db = db_connect();
			$nume_contact = $_GET['nume_contact'];
			$contact_sex = $_GET['contact_sex'];
            $tel_fix = $_GET['tel_fix'];
			$email = $_GET['email'];
			
			$optiune = $_GET['optiune'];
			$index = $_GET['index'];
			$id_firma = $_GET['id_firma'];
			
			$raspuns_error = "";
			
			if (!validPrintable($nume_contact, 1, CT_MAX_NUME_CONTACT_LENGTH)) 
				 $raspuns_error .= "td_contact_nume;";
						
			if ($contact_sex != 0 && $contact_sex != 1) 
				 $raspuns_error .= "td_contact_sex;";

		
			if (!validPhone($contact_tel_fix, false)) 
				 $raspuns_error .= "td_contact_tel_fix;";		
	
			if (!validMail($contact_email) && $contact_email != '') 
				 $raspuns_error .= "td_contact_email;";

			
			if (strlen($raspuns_error) > 0)
                echo 'VALIDATE;' . $raspuns_error;
            else {
            	if ($optiune == 'insert') {
            		//adaugare contact nou
            		try {
	            		$contact = new Contact($id_firma, $nume_contact, $contact_sex, null, $tel_fix, null, null, $email);
	            		$contact->queryInsertContact($db);
            		}
            		catch (QueryInsertContactException $e) {
            			echo "ERROR;" . ERROR_INSERT_CONTACT;
            		}
            	}
            	else {
            		//editare
            		try {
	            		$contact = Contact::queryLoadContact($db, $index);
	            		$contact->nume = $nume_contact;
	            		$contact->sex = $contact_sex;
	            		$contact->email = $email;
	            		$contact->tel_fix = $tel_fix;
	            		$contact->queryUpdateContact($db);
            		}
            		catch (QueryUpdateContactException $e) {
            			echo "ERROR;" . ERROR_UPDATE_CONTACT;
            		}
            		catch (QueryLoadContactException $e) {
            			echo "ERROR;" . ERROR_LOAD_CONTACT;
            		}
            	}
            }                                			
			break;        	
        }
        
        case "ajax_delete_render_dash": {
            
            $db = db_connect();
            $tip = $_GET['tip'];
            $dashboard_id = $_GET['id'];
            $id_client = $_SESSION['user_logat']->id_client;
            
            try {
                Dashboard::queryDeleteByID($db, $id_client, $dashboard_id);
            }
            catch (QueryDeleteDashboardException $e) {
                echo "ERROR;" . ERROR_DELETE_DASHBOARD_BY_ID;
            }
            render_dashboard($tip, $id_client);
            
            break;
        }
        
        case "ajax_date_confirm": {
            require_once('server_validations.inc');
            $db = db_connect();
            $id_adresa = $_GET['id_adresa'];
            $id_comanda = $_GET['id_comanda'];
            $data_efectiva = getDateForServer($_GET['data_efectiva'], true);
            
            $raspuns_error = "";
            
            $aux = checkServerDateTime($data_efectiva);
                if ($aux == FALSE) {
                    $raspuns_error .= "td_data_efectiva;";
                }
                
            if (strlen($raspuns_error) > 0)
                echo "VALIDATE;".$raspuns_error;
            else {    
                try {
                    Address::queryUpdateAddressConfirmDate($db, $id_comanda, $id_adresa, $data_efectiva);
                }
                catch (QueryUpdateAddressException $e) {
                    echo "ERROR;" . ERROR_UPDATE_ADRESS_CONFIRM_DATE;
                }
            }
            break;
        }
        
        case "render_incarcari_descarcari_urgente": {
            $tip_adresa = $_GET['tip_adresa'];
            $id_lista = $_GET['id_lista'];
        	render_comenzi_incarcare_descarcare_urgente($_SESSION['client_logat']->id_client
                    , $_SESSION['user_logat']->comenzi_descarcari_per_pag, $tip_adresa, $id_lista);
            break;
        }
	}
	flush();
?>
